/*
 * The main menu screen
 */

#include <allegro.h>
#include "menu.h"
#include "wingfx.h"
#include "main.h"
#include "resources.h"

#define WND_BUTTONS_X 70
#define WND_BUTTONS_Y 260
#define WND_SUB_X (WND_BUTTONS_X+156)
#define WND_HOST_Y WND_BUTTONS_Y
#define WND_CLIENT_Y (WND_BUTTONS_Y+24)

Window menu_wnd_buttons;
Window menu_wnd_host;
Window menu_wnd_client;
Button menu_btn_host;
Button menu_btn_client;
Button menu_btn_options;
Button menu_btn_exit;
Button menu_btn_host_ok;
Button menu_btn_client_ok;
Textfield menu_txt_host_nickname;
Textfield menu_txt_client_hoststr;
Textfield menu_txt_client_nickname;
enum { MENU_VIEW_DEFAULT, MENU_VIEW_HOST, MENU_VIEW_CLIENT } menu_view;

void menu_btnclick_host ();
void menu_btnclick_client ();
void menu_btnclick_options ();
void menu_btnclick_exit ();
void menu_btnclick_host_ok ();
void menu_btnclick_client_ok ();
	
void menu_init ()
{
	clear_keybuf ();
	show_mouse (screen);
	menu_wnd_buttons   = (Window) { bmp.winset, WINGFX_SET, WND_BUTTONS_X, WND_BUTTONS_Y, 18, 13 };
	menu_wnd_host      = (Window) { bmp.winset, WINGFX_SET, WND_SUB_X, WND_HOST_Y, 27, 8 };
	menu_wnd_client    = (Window) { bmp.winset, WINGFX_SET, WND_SUB_X, WND_CLIENT_Y, 27, 11 };
	menu_btn_host      = (Button) { bmp.winset, WINGFX_SET, WND_BUTTONS_X+12, WND_BUTTONS_Y+12, 15, 0, 0, fnt.sans, "Host Game" };
	menu_btn_client    = (Button) { bmp.winset, WINGFX_SET, WND_BUTTONS_X+12, WND_BUTTONS_Y+34, 15, 0, 0, fnt.sans, "Join Game" };
	menu_btn_options   = (Button) { bmp.winset, WINGFX_SET, WND_BUTTONS_X+12, WND_BUTTONS_Y+56, 15, 0, 0, fnt.sans, "Options" };
	menu_btn_exit      = (Button) { bmp.winset, WINGFX_SET, WND_BUTTONS_X+12, WND_BUTTONS_Y+78, 15, 0, 0, fnt.sans, "Quit" };
	menu_btn_host_ok   = (Button) { bmp.winset, WINGFX_SET, WND_SUB_X+80, WND_HOST_Y+36, 5, 0, 0, fnt.sans, "OK" };
	menu_btn_client_ok = (Button) { bmp.winset, WINGFX_SET, WND_SUB_X+80, WND_CLIENT_Y+60, 5, 0, 0, fnt.sans, "OK" };
	menu_txt_host_nickname   = (Textfield) { bmp.winset, WINGFX_SET, WND_SUB_X+80, WND_HOST_Y+12, 15, fnt.sans, "" };
	menu_txt_client_hoststr  = (Textfield) { bmp.winset, WINGFX_SET, WND_SUB_X+80, WND_CLIENT_Y+12, 15, fnt.sans, "" };
	menu_txt_client_nickname = (Textfield) { bmp.winset, WINGFX_SET, WND_SUB_X+80, WND_CLIENT_Y+36, 15, fnt.sans, "" };
	menu_view = MENU_VIEW_DEFAULT;
}

void menu_update ()
{
	button_mouseevent (&menu_btn_host   , mouse_x, mouse_y, mouse_b & 1);
	button_mouseevent (&menu_btn_client , mouse_x, mouse_y, mouse_b & 1);
	button_mouseevent (&menu_btn_options, mouse_x, mouse_y, mouse_b & 1);
	button_mouseevent (&menu_btn_exit   , mouse_x, mouse_y, mouse_b & 1);
	
	if (button_was_clicked(&menu_btn_host   )) menu_btnclick_host ();
	if (button_was_clicked(&menu_btn_client )) menu_btnclick_client ();
	if (button_was_clicked(&menu_btn_options)) menu_btnclick_options ();
	if (button_was_clicked(&menu_btn_exit   )) menu_btnclick_exit ();
	
	switch (menu_view) {
		
	case MENU_VIEW_HOST:
		button_mouseevent (&menu_btn_host_ok, mouse_x, mouse_y, mouse_b & 1);
		if (button_was_clicked(&menu_btn_host_ok)) menu_btnclick_host_ok ();
		break;
	
	case MENU_VIEW_CLIENT:
		button_mouseevent (&menu_btn_client_ok, mouse_x, mouse_y, mouse_b & 1);
		if (button_was_clicked(&menu_btn_client_ok)) menu_btnclick_client_ok ();
		break;
		
	default:
		break;
		
	}
}

void menu_draw ()
{
	acquire_bitmap(buffer);
	
	// Background image
	stretch_blit (bmp.menu, buffer, 
	              0, 0, bmp.menu->w, bmp.menu->h, 
	              0, 0, buffer->w, buffer->h);
	
	// Window and buttons
	window_draw (buffer, &menu_wnd_buttons);
	button_draw (buffer, &menu_btn_options, 0);
	button_draw (buffer, &menu_btn_exit, 0);
	
	switch (menu_view) {
		
	case MENU_VIEW_HOST:
		window_draw (buffer, &menu_wnd_host);
		textout_ex (buffer, fnt.sans, "Your Name:", WND_SUB_X+12, WND_HOST_Y+14, -1, -1);
		textfield_draw (buffer, &menu_txt_host_nickname);
		button_draw (buffer, &menu_btn_host_ok, 0);
		button_draw (buffer, &menu_btn_host, 1);
		button_draw (buffer, &menu_btn_client, 0);
		break;
		
	case MENU_VIEW_CLIENT:
		window_draw (buffer, &menu_wnd_client);
		textout_ex (buffer, fnt.sans, "Server IP:", WND_SUB_X+12, WND_CLIENT_Y+14, -1, -1);
		textout_ex (buffer, fnt.sans, "Your Name:", WND_SUB_X+12, WND_CLIENT_Y+38, -1, -1);
		textfield_draw (buffer, &menu_txt_client_hoststr);
		textfield_draw (buffer, &menu_txt_client_nickname);
		button_draw (buffer, &menu_btn_client_ok, 0);
		button_draw (buffer, &menu_btn_host, 0);
		button_draw (buffer, &menu_btn_client, 1);
		break;
		
	default:
		button_draw (buffer, &menu_btn_host, 0);
		button_draw (buffer, &menu_btn_client, 0);
		break;
		
	}
	
	release_bitmap(buffer);
}

void menu_cleanup ()
{
	show_mouse (NULL);
}

void menu_btnclick_host ()
{
	if (menu_view == MENU_VIEW_HOST) {
		menu_view = MENU_VIEW_DEFAULT;
	} else {
		menu_view = MENU_VIEW_HOST;
	}
}

void menu_btnclick_client ()
{
	if (menu_view == MENU_VIEW_CLIENT) {
		menu_view = MENU_VIEW_DEFAULT;
	} else {
		menu_view = MENU_VIEW_CLIENT;
	}
}

void menu_btnclick_options ()
{
	
}

void menu_btnclick_exit ()
{
	change_state (STATE_UNDEFINED); // This will cause app exit
}

void menu_btnclick_host_ok ()
{
	
}

void menu_btnclick_client_ok ()
{
	change_state (STATE_LOBBY);
}
